package com.wch.spit.service;

import com.wch.spit.dao.SpitDao;
import com.wch.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;
import java.util.List;

/**
 * 业务逻辑层
 */
@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询全部记录
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 根据主键查询实体
     * @param id
     * @return
     */
    public Spit findById(String id){
        Spit spit = spitDao.findById(id).get();
        return spit;
    }

    /**
     * 增加
     * @param spit
     */
    public void add(Spit spit){
      /*  spit.set_id(idWorker.nextId()+"");
        spitDao.save(spit);*/

        //修改增加的逻辑
        spit.set_id(idWorker.nextId()+"");  //id
        spit.setPublishtime(new Date());   //发布日期
        spit.setVisits(0);  //浏览量
        spit.setShare(0);  //分享数
        spit.setThumbup(0);  //点赞数
        spit.setComment(0);  //回复数
        spit.setState("1"); //状态

        //如果存在上级评论，则上级评论的评论数自加
        if(null != spit.getParentid() && !"".equals(spit.getParentid().toString())){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment",1);
            mongoTemplate.updateFirst(query,update,"spit");

        }
        spitDao.save(spit);
    }

    /**
     * 修改
     * @param spit
     */
    public void update(Spit spit){
        spitDao.save(spit);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(String id){
        spitDao.deleteById(id);
    }

    /**
     * 自定义：根据上级ID查询列表信息
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentid,int page,int size){
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return spitDao.findByParentid(parentid,pageRequest);
    }


    /**
     * 点赞
     * @param id
     */
    public void updateThumbup(String id){
        //实现起来比较简单，但是执行效率并不高，因为我只需要将点赞数加1就可 以了，没必要查询出所有字段修改后再更新所有字段
        /*Spit spit = spitDao.findById(id).get();
        spit.setThumbup(spit.getThumbup()+1);
        spitDao.save(spit);*/

        //新方法，mongodb的操作
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }

    /**
     * 增加浏览量
     * @param id
     */
    public void updateVisits(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("visits",1);
        mongoTemplate.updateFirst(query,update,"spit");

    }

    /**
     * 增加分享数
     * @param id
     */
    public void updateShare(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("share",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }


}
